<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>cookie</title>
    <script>
        window.onload = function (ev) {
            addCookie("age", "11", 1, "/", "127.0.0.1");

            function addCookie(key, value, day, path, domain) {
                //1.处理默认保存路径
                var index = window.location.pathname.lastIndexOf("/");
                var currentPath = window.location.pathname.slice(0, index);
                path = path || currentPath;
                //2.处理默认保存的domian
                domain = domain || document.domain;
                //3.处理默认过期时间
                if (!day) {
                    document.cookie = key + "=" + value + ";path=" + path + ";domain=" + domain + ";";
                } else {
                    var date = new Date();
                    date.setDate(date.getDate() + day);
                    document.cookie = key + "=" + value + ";expires=" + date.toGMTString() + ";path=" + path +
                        ";domain=" + domain + ";";
                }

            }

            function getCookie(key) {
                var res = document.cookie.split(";");
                for (var i = 0; i < res.length; i++) {
                    var temp = res[i].split("=");
                    if (temp[0].trim() === key) {
                        return temp[1];
                    }
                }
            }
            getCookie("age");
            function delCookie(key,path){
                addCookie(key,getCookie(key),-1,path);
            }
            delCookie("age","/");
        }
    </script>

</head>

<body>


</body>

</html>